package sis.bosanova
import grails.plugins.springsecurity.Secured
class EstudianteController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }
    @Secured(['ROLE_ADMIN','ROLE_DOCENTE'])
    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [estudianteInstanceList: Estudiante.list(params), estudianteInstanceTotal: Estudiante.count()]
    }
    @Secured(['ROLE_ADMIN'])
    def create = {
        def estudianteInstance = new Estudiante()
        estudianteInstance.properties = params
        return [estudianteInstance: estudianteInstance]
    }
    @Secured(['ROLE_ADMIN'])
    def save = {
        def estudianteInstance = new Estudiante(params)
        if (estudianteInstance.save(flush: true)) {
            def userRole = Roll.findByAuthority('ROLE_ESTUDIANTE') ?: new Roll(authority: 'ROLE_ESTUDIANTE').save(failOnError: true)
            CuentaRoll.create estudianteInstance, userRole , true
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'estudiante.label', default: 'Estudiante'), estudianteInstance.id])}"
            redirect(action: "show", id: estudianteInstance.id)
        }
        else {
            render(view: "create", model: [estudianteInstance: estudianteInstance])
        }
    }
    @Secured(['ROLE_ADMIN','ROLE_DOCENTE'])
    def show = {
        def estudianteInstance = Estudiante.get(params.id)
        if (!estudianteInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'estudiante.label', default: 'Estudiante'), params.id])}"
            redirect(action: "list")
        }
        else {
            [estudianteInstance: estudianteInstance]
        }
    }
    @Secured(['ROLE_ADMIN'])
    def edit = {
        def estudianteInstance = Estudiante.get(params.id)
        if (!estudianteInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'estudiante.label', default: 'Estudiante'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [estudianteInstance: estudianteInstance]
        }
    }
    @Secured(['ROLE_ADMIN'])
    def update = {
        def estudianteInstance = Estudiante.get(params.id)
        if (estudianteInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (estudianteInstance.version > version) {
                    
                    estudianteInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'estudiante.label', default: 'Estudiante')] as Object[], "Another user has updated this Estudiante while you were editing")
                    render(view: "edit", model: [estudianteInstance: estudianteInstance])
                    return
                }
            }
            estudianteInstance.properties = params
            if (!estudianteInstance.hasErrors() && estudianteInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'estudiante.label', default: 'Estudiante'), estudianteInstance.id])}"
                redirect(action: "show", id: estudianteInstance.id)
            }
            else {
                render(view: "edit", model: [estudianteInstance: estudianteInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'estudiante.label', default: 'Estudiante'), params.id])}"
            redirect(action: "list")
        }
    }
    @Secured(['ROLE_ADMIN'])
    def delete = {
        def estudianteInstance = Estudiante.get(params.id)
        if (estudianteInstance) {
            try {
                estudianteInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'estudiante.label', default: 'Estudiante'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'estudiante.label', default: 'Estudiante'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'estudiante.label', default: 'Estudiante'), params.id])}"
            redirect(action: "list")
        }
    }
}
